package com.reporting.yb.module.system.service.reportingfile;

import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;
import com.reporting.yb.module.system.controller.admin.reportingfile.vo.ReportingFilePageReqVO;
import com.reporting.yb.module.system.controller.admin.reportingfile.vo.ReportingFileSaveReqVO;
import com.reporting.yb.module.system.dal.dataobject.reportingfile.ReportingFileDO;
import com.reporting.yb.module.system.dal.mysql.reportingfile.ReportingFileMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.reporting.yb.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.reporting.yb.module.system.enums.ErrorCodeConstants.REPORTING_FILE_NOT_EXISTS;

/**
 * 投诉文件 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ReportingFileServiceImpl implements ReportingFileService {

    @Resource
    private ReportingFileMapper reportingFileMapper;

    @Override
    public String createReportingFile(ReportingFileSaveReqVO createReqVO) {
        // 插入
        ReportingFileDO reportingFile = BeanUtils.toBean(createReqVO, ReportingFileDO.class);
        reportingFileMapper.insert(reportingFile);
        // 返回
        return reportingFile.getId();
    }

    @Override
    public void updateReportingFile(ReportingFileSaveReqVO updateReqVO) {
        // 校验存在
        validateReportingFileExists(updateReqVO.getId());
        // 更新
        ReportingFileDO updateObj = BeanUtils.toBean(updateReqVO, ReportingFileDO.class);
        reportingFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportingFile(String id) {
        // 校验存在
        validateReportingFileExists(id);
        // 删除
        reportingFileMapper.deleteById(id);
    }

    private void validateReportingFileExists(String id) {
        if (reportingFileMapper.selectById(id) == null) {
            throw exception(REPORTING_FILE_NOT_EXISTS);
        }
    }

    @Override
    public ReportingFileDO getReportingFile(String id) {
        return reportingFileMapper.selectById(id);
    }

    @Override
    public PageResult<ReportingFileDO> getReportingFilePage(ReportingFilePageReqVO pageReqVO) {
        return reportingFileMapper.selectPage(pageReqVO);
    }

}